package com.eascs.archive.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.archive.entity.ArchiveBorrowEntity;
import com.eascs.archive.entity.ArchiveRecordEntity;
import com.eascs.archive.untils.ArchiveConstants;

@Repository
public class ArchiveBorrowDao extends BaseO2oDao{
	
	public PageResult<Object[]> queryListPage(ArchiveBorrowEntity entity, int pageSize, int pageIndex){
		if (entity == null) {
			entity=new ArchiveBorrowEntity();
		}
		StringBuffer sb=new StringBuffer();
		sb.append(" SELECT bid,bcode,create_time,proposer,DATE_FORMAT(pre_return_time,'%Y-%m-%d'),DATE_FORMAT(return_time,'%Y-%m-%d'),wfstate FROM o2o_archive_borrow where state=1 ");
		
		if (StringUtils.isNotBlank(entity.getBcode())) {
			sb.append(" AND bcode='"+entity.getBcode()+"'");
		}
		
		if (StringUtils.isNotBlank(entity.getLoanapply_no())) {
			sb.append(" AND loanapply_no='"+entity.getLoanapply_no()+"'");
		}
		sb.append(" ORDER BY create_time DESC");
		SqlPageQuery<Object[]> sqlPageQuery=new SqlPageQuery<Object[]>(Object[].class, pageSize, pageIndex,sb.toString());
		return this.findDataWithPage(sqlPageQuery);
	}
	
	//查询全部，包括已删除的
	public List<ArchiveBorrowEntity> queryAllListByloanapplyNo(String loanapplyNo){
		if (StringUtils.isBlank(loanapplyNo)) {
			return null;
		}
		String sql="select * from o2o_archive_borrow where loanapply_no=?";
		return this.findEntitys(sql, ArchiveBorrowEntity.class, loanapplyNo);
	}
	
	public int updateArchiveBorrow(ArchiveBorrowEntity entity){
		if(entity == null)
			return 0;
		String sql ="update o2o_archive_borrow set cw_contentJson=?,ck_contentJson=? where bid =?";
		return this.execute(sql, new Object[]{entity.getCw_contentJson(),entity.getCk_contentJson(),entity.getBid()});
	}
	
	public int saveSendBorrowInfo(ArchiveBorrowEntity entity){
		if(entity == null)
			return 0;
		String sql ="update o2o_archive_borrow set send_time=?,send_company=?,send_no=? where bid =?";
		return this.execute(sql, new Object[]{entity.getSend_time(),entity.getSend_company(),entity.getSend_no(),entity.getBid()});
	}
	
	public int updateArchiveBorrowRecordById(ArchiveBorrowEntity entity){
		if (entity == null) {
			return 0;
		}
		entity.setUpdate_time(new Date());
		return this.updateById(entity);
	}
	
	public int updateBorrowState(String bid,String wfstate){
		if (StringUtils.isBlank(bid) || StringUtils.isBlank(wfstate)) {
			return 0;
		}
		String sql="update o2o_archive_borrow set wfstate=? where bid=?";
		return this.execute(sql, new Object[]{wfstate,bid});
	}
	
	public ArchiveBorrowEntity queryArchiveBorrowById(String bid){
		String sql="select * from o2o_archive_borrow where bid=?";
		return this.findEntity(sql, ArchiveBorrowEntity.class, bid);
	}
	
	//是否已发起归档流程
	public ArchiveRecordEntity queryArchiveExist(String loanapplyno){
		String sql="SELECT * FROM o2o_archive_record WHERE loanapply_no=? AND form_type_id=? AND content_json IS NOT NULL";
		return this.findEntity(sql, ArchiveRecordEntity.class, loanapplyno,ArchiveConstants.FORMTYPE_ARCHIVE_INFO);
	}
	
	public int deleteArchiveBorrowById(String bid){
		if (StringUtils.isBlank(bid)) {
			return 0;
		}
		String sql="update o2o_archive_borrow set state =? WHERE bid =? and wfstate =?";
		return o2oJdbcSession.execute(sql, new Object[]{ArchiveBorrowEntity.state_n, bid,ArchiveBorrowEntity.wf_state_new});
	}
	
	public int insertArchiveBorrow(ArchiveBorrowEntity entity){
		if (entity == null) {
			return 0;
		}
		Date date=new Date();
		entity.setCreate_time(date);
		entity.setUpdate_time(date);
		entity.setState(ArchiveBorrowEntity.state_y);
		entity.setWfstate(ArchiveBorrowEntity.wf_state_new);
		return o2oJdbcSession.insert(entity);
	}
	
}
